﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;
using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI
{
    public partial class FrmEditDeposit : BaseForm
    {
        public WHC.Hotel.Entity.RoomInfo RoomInfo = null;
        private OrderInfo orderInfo = null;

        public FrmEditDeposit()
        {
            InitializeComponent();
        }

        private void FrmEditDeposit_Load(object sender, EventArgs e)
        {
            if (RoomInfo != null)
            {
                this.txtPayType.Items.Clear();
                this.txtPayType.Items.AddRange(DictItemUtil.GetPayType());
                if (this.txtPayType.Items.Count > 0)
                {
                    this.txtPayType.SelectedIndex = 0;
                }

                orderInfo = BLLFactory<Order>.Instance.GetCurrentOrder(RoomInfo.RoomNo);
                if (orderInfo != null)
                {
                    this.lblOrderNo.Text = orderInfo.OrderNo;
                    this.lblRoomNo.Text = RoomInfo.RoomNo;
                    this.lblName.Text = orderInfo.CustomerName;

                    this.txtAddPrelived.Focus();
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                this.DialogResult = DialogResult.None;
                decimal addedDeposit = Convert.ToDecimal(this.txtAddDeposit.Text);
                orderInfo.DepositMoney += addedDeposit;
                orderInfo.PreLivedDays += Convert.ToInt32(this.txtAddPrelived.Text);
                orderInfo.PayType = this.txtPayType.Text;
                orderInfo.PreOffDate = orderInfo.PreOffDate.AddDays(Convert.ToInt32(this.txtAddPrelived.Text));

                BLLFactory<Order>.Instance.Update(orderInfo, orderInfo.ID.ToString());
                AddMoneyDetail(addedDeposit, this.txtPayType.Text);

                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                MessageUtil.ShowError(ex.Message);
            }
        }

        /// <summary>
        /// 增加收银记录
        /// </summary>
        /// <returns></returns>
        private void AddMoneyDetail(decimal depositMoney, string payType)
        {
            if (depositMoney != 0M)
            {
                MoneyDetailInfo info = new MoneyDetailInfo();
                info.Cash = depositMoney;
                info.CashierType = "押金收银";
                info.Creator = Portal.gc.LoginInfo.FullName;
                info.PayDate = DateTime.Now;
                info.Customer_ID = orderInfo.Customer_ID;
                info.CustomerName = orderInfo.CustomerName;
                info.RoomNo = orderInfo.RoomNo;
                info.PayType = payType;
                info.Note = "续/退押金操作";

                BLLFactory<MoneyDetail>.Instance.Insert(info);
            }
        }
    }
}
